﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
using System.Windows;
using System.Runtime.InteropServices;

namespace acr_server
{
    
    public partial class MainForm : Form
    {

        ImageReceiver imgRecv;
        StringWriter logStream;
        bool isRunnig;
        Thread logThread;
        public MainForm()
        {
            InitializeComponent();
            oldState = WindowState;
            isRunnig = true;
            logStream = new StringWriter();
            imgRecv = new ImageReceiver(logStream);
            logThread = new Thread(delegate()
                {
                    while (isRunnig)
                    {
                        SetText(logStream.ToString());
                        if (imgRecv.CurrentImage != null  && !imgRecv.CurrentImage.Equals(pb.Image))
                            SetImage((Image)imgRecv.CurrentImage);
                        Thread.Sleep(100);
                    }
                });
            logThread.Start();
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            isRunnig = false;
            logThread.Abort();
            notifyIcon1.Visible = false;
            imgRecv.CloseSockets();
        }
        private void SetText(string text)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.tb.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else
            {
                this.tb.Text = text;
            }
        }
        // This delegate enables asynchronous calls for setting
        // the text property on a TextBox control.
        delegate void SetTextCallback(string text);

        private void SetImage(Image img)
        {
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.pb.InvokeRequired)
            {
                SetImageCallback d = new SetImageCallback(SetImage);
                this.Invoke(d, new object[] { img });
            }
            else
            {
                this.pb.Image = img;
            }
        }
        // This delegate enables asynchronous calls for setting
        // the text property on a TextBox control.
        delegate void SetImageCallback(Image img);

        private void tb_TextChanged(object sender, EventArgs e)
        {
            tb.SelectionStart = tb.Text.Length;
            tb.ScrollToCaret();
            tb.Refresh();
        }
        FormWindowState oldState;

        private void MainForm_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                ShowInTaskbar = false;
                FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
            }
            else
            {
                oldState = WindowState;
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
            ShowInTaskbar = true;
            WindowState = oldState;
            Activate();
        }

        private void MainForm_Shown(object sender, EventArgs e)
        {
            if (!imgRecv.Success)
                this.Close();
        }

    }
}
